package com.jlrdb.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.jlrdb.model.TvBroadcast;
import com.jlrdb.model.TvBroadcasts;
import com.jlrdb.service.TvBroadcastService;

@SuppressWarnings("serial")
public class TvBroadcastServlet extends HttpServlet {
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		TvBroadcastService tvBroadcastService = new TvBroadcastService();
		if (action.equals("viewTvChoices"))
		{
			UserService userService = UserServiceFactory.getUserService();
			User user = userService.getCurrentUser();
			String Role;
			String domain = "temp";
			String identity = user.getEmail();
			if (identity.contains("jaguarlandrover.com"))
				domain = "jaguarlandrover.com";
			if (domain.equalsIgnoreCase("jaguarlandrover.com"))
			{
				if (identity.equalsIgnoreCase("gagarwa2@jaguarlandrover.com") || identity.equalsIgnoreCase("rholenar@jaguarlandrover.com") || identity.equalsIgnoreCase("poreill3@jaguarlandrover.com")
						|| identity.equalsIgnoreCase("sarcot@jaguarlandrover.com"))
					Role = "Admin";
				else
					Role = "Viewer";
				HttpSession session = request.getSession();
				session.setAttribute("Role", Role);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/viewTvBroadcastByChoice.jsp");
				rd.forward(request, response);
			} else
			{
				String message = "Please LOGIN with a valid JLR domain account.";
				request.setAttribute("message", message);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/message.jsp");
				rd.forward(request, response);
			}
		} else if (action.equals("addTvBroadcast"))
		{
			TvBroadcast tvBroadcast = new TvBroadcast();
			tvBroadcast.setGeographic_Region(request.getParameter("geographic_region"));
			tvBroadcast.setCountry(request.getParameter("country"));
			tvBroadcast.setDtt_Standard(request.getParameter("dtt_standard"));
			tvBroadcast.setDtt_Status(request.getParameter("dtt_status"));
			tvBroadcast.setAnalog_Standard(request.getParameter("analog_standard"));
			tvBroadcast.setAso_Date_Start(request.getParameter("aso_date_start"));
			tvBroadcast.setAso_Date_Completion(request.getParameter("aso_date_completion"));
			tvBroadcast.setAso_Status(request.getParameter("aso_status"));
			tvBroadcast.setDso_Date_Start(request.getParameter("dso_date_start"));
			tvBroadcast.setDso_Date_Completion(request.getParameter("dso_date_completion"));
			tvBroadcast.setDso_Status(request.getParameter("dso_status"));
			tvBroadcast.setNext_Transition_Planned(request.getParameter("next_transition_planned"));
			tvBroadcast.setStandard_Transition_Into(request.getParameter("standard_transition_into"));
			tvBroadcast.setStatus(request.getParameter("status"));
			tvBroadcast.setStart_Date(request.getParameter("start_date"));
			tvBroadcast.setNotes(request.getParameter("notes"));
			tvBroadcast.setStatus_Confirmed(request.getParameter("status_confirmed"));
			DateFormat df = new SimpleDateFormat("dd/MM/yy");
			String formattedDate = df.format(new Date());
			tvBroadcast.setLast_Date_Verified(formattedDate);
			tvBroadcast.setVideo_Format_Supported(request.getParameter("video_format_supported"));
			tvBroadcast.setOperators(request.getParameter("operators"));
			tvBroadcast.setMux_Details(request.getParameter("mux_details"));
			tvBroadcast.setDtt_Coverage(request.getParameter("dtt_coverage"));
			tvBroadcast.setHd_Present(request.getParameter("hd_present"));
			tvBroadcast.setAudio_Formats(request.getParameter("audio_formats"));
			tvBroadcast.setFrequency_Bands(request.getParameter("frequency_bands"));
			tvBroadcast.setInteractive_Standards(request.getParameter("interactive_standard"));
			tvBroadcast.setChannel_Bandwidth(request.getParameter("channel_bandwidth"));
			tvBroadcast.setCarrier_Type(request.getParameter("carrier_type"));
			tvBroadcast.setModulation(request.getParameter("modulation"));
			tvBroadcast.setBroadcast_Authority(request.getParameter("broadcast_authority"));
			tvBroadcast.setItu_Region(request.getParameter("itu_region"));
			tvBroadcast.setBroadcasters(request.getParameter("broadcasters"));
			tvBroadcast.setSource_Of_Reference(request.getParameter("source_of_reference"));
			tvBroadcast = tvBroadcastService.checkEmptyValues(tvBroadcast);
			int result = tvBroadcastService.addTvBroadcast(tvBroadcast);
			if (result == 0)
			{
				request.setAttribute("tvBroadcast", tvBroadcast);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/tvBroadcastAdded.jsp");
				rd.forward(request, response);
			}
		} else if (action.equals("viewTvBroadcastsNew"))
		{
			String geographic_region = (request.getParameter("geographic_region"));
			String country = (request.getParameter("country"));
			String dtt_standard = (request.getParameter("dtt_standard"));
			String dtt_status = (request.getParameter("dtt_status"));
			String analog_standard = (request.getParameter("analog_standard"));
			String aso_status = (request.getParameter("aso_status"));
			String dso_status = (request.getParameter("dso_status"));
			String next_transition_planned = (request.getParameter("next_transition_planned"));
			String standard_transition_into = (request.getParameter("standard_transition_into"));
			String status = (request.getParameter("status"));
			String status_confirmed = (request.getParameter("status_confirmed"));
			String hd_present = (request.getParameter("hd_present"));
			String interactive_standard = (request.getParameter("interactive_standard"));
			TvBroadcasts tvBroadcasts = new TvBroadcasts();
			TvBroadcast tvQuery = new TvBroadcast();
			tvQuery.setCountry(country);
			tvQuery.setGeographic_Region(geographic_region);
			tvQuery.setDtt_Standard(dtt_standard);
			tvQuery.setDtt_Status(dtt_status);
			tvQuery.setAnalog_Standard(analog_standard);
			tvQuery.setAso_Status(aso_status);
			tvQuery.setDso_Status(dso_status);
			tvQuery.setNext_Transition_Planned(next_transition_planned);
			tvQuery.setStandard_Transition_Into(standard_transition_into);
			tvQuery.setStatus(status);
			tvQuery.setStatus_Confirmed(status_confirmed);
			tvQuery.setHd_Present(hd_present);
			tvQuery.setInteractive_Standards(interactive_standard);
			tvBroadcasts = tvBroadcastService.viewTvBroadcastsNew(tvQuery);
			if (tvBroadcasts.getTvBroadcasts().size() == 0)
			{
				String message = "Sorry no data available for given criteria:-";
				if (geographic_region.length() != 0)
					message += "\ngeographic_region: " + geographic_region;
				if (country.length() != 0)
					message += "\ncountry: " + country;
				if (dtt_standard.length() != 0)
					message += "\ndtt_standard: " + dtt_standard;
				if (dtt_status.length() != 0)
					message += "\ndtt_status: " + dtt_status;
				if (analog_standard.length() != 0)
					message += "\nanalog_standard: " + analog_standard;
				if (aso_status.length() != 0)
					message += "\naso_status: " + aso_status;
				if (dso_status.length() != 0)
					message += "\ndso_status: " + dso_status;
				if (next_transition_planned.length() != 0)
					message += "\nnext_transition_planned: " + next_transition_planned;
				if (standard_transition_into.length() != 0)
					message += "\nstandard_transition_into: " + standard_transition_into;
				if (status.length() != 0)
					message += "\nstatus: " + status;
				if (status_confirmed.length() != 0)
					message += "\nstatus_confirmed: " + status_confirmed;
				if (hd_present.length() != 0)
					message += "\nhd_present: " + hd_present;
				if (interactive_standard.length() != 0)
					message += "\ninteractive_standard: " + interactive_standard;
				request.setAttribute("message", message);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/message.jsp");
				rd.forward(request, response);
			} else if (country.length() != 0)
			{
				request.setAttribute("tvBroadcasts", tvBroadcasts);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/viewTvFullDetails.jsp");
				rd.forward(request, response);
			} else
			{
				if (geographic_region.length() != 0)
					request.setAttribute("geographic_region", geographic_region);
				else
					request.setAttribute("geographic_region", "none");
				request.setAttribute("tvBroadcasts", tvBroadcasts);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/viewTvBroadcasts.jsp");
				rd.forward(request, response);
			}
		} else if (action.equals("getThisBroadcast"))
		{
			long broadcastId = Long.parseLong(request.getParameter("broadcastId"));
			TvBroadcast tvBroadcast = new TvBroadcast();
			tvBroadcast = tvBroadcastService.getThisBroadcast(broadcastId);
			request.setAttribute("tvBroadcast", tvBroadcast);
			RequestDispatcher rd = request.getRequestDispatcher("jsp/updateTvBroadcast.jsp");
			rd.forward(request, response);
		} else if (action.equals("updateTvBroadcast"))
		{
			TvBroadcast tvBroadcast = new TvBroadcast();
			// tvBroadcast.setGeographic_Region(request.getParameter("geographic_region"));
			// tvBroadcast.setCountry(request.getParameter("country"));
			tvBroadcast.setDtt_Standard(request.getParameter("dtt_standard"));
			tvBroadcast.setDtt_Status(request.getParameter("dtt_status"));
			tvBroadcast.setAnalog_Standard(request.getParameter("analog_standard"));
			tvBroadcast.setAso_Date_Start(request.getParameter("aso_date_start"));
			tvBroadcast.setAso_Date_Completion(request.getParameter("aso_date_completion"));
			tvBroadcast.setAso_Status(request.getParameter("aso_status"));
			tvBroadcast.setDso_Date_Start(request.getParameter("dso_date_start"));
			tvBroadcast.setDso_Date_Completion(request.getParameter("dso_date_completion"));
			tvBroadcast.setDso_Status(request.getParameter("dso_status"));
			tvBroadcast.setNext_Transition_Planned(request.getParameter("next_transition_planned"));
			tvBroadcast.setStandard_Transition_Into(request.getParameter("standard_transition_into"));
			tvBroadcast.setStatus(request.getParameter("status"));
			tvBroadcast.setStart_Date(request.getParameter("start_date"));
			tvBroadcast.setNotes(request.getParameter("notes"));
			tvBroadcast.setStatus_Confirmed(request.getParameter("status_confirmed"));
			DateFormat df = new SimpleDateFormat("dd/MM/yy");
			String formattedDate = df.format(new Date());
			tvBroadcast.setLast_Date_Verified(formattedDate);
			tvBroadcast.setVideo_Format_Supported(request.getParameter("video_format_supported"));
			tvBroadcast.setOperators(request.getParameter("operators"));
			tvBroadcast.setMux_Details(request.getParameter("mux_details"));
			tvBroadcast.setDtt_Coverage(request.getParameter("dtt_coverage"));
			tvBroadcast.setHd_Present(request.getParameter("hd_present"));
			tvBroadcast.setAudio_Formats(request.getParameter("audio_formats"));
			tvBroadcast.setFrequency_Bands(request.getParameter("frequency_bands"));
			tvBroadcast.setInteractive_Standards(request.getParameter("interactive_standard"));
			tvBroadcast.setChannel_Bandwidth(request.getParameter("channel_bandwidth"));
			tvBroadcast.setCarrier_Type(request.getParameter("carrier_type"));
			tvBroadcast.setModulation(request.getParameter("modulation"));
			tvBroadcast.setBroadcast_Authority(request.getParameter("broadcast_authority"));
			tvBroadcast.setItu_Region(request.getParameter("itu_region"));
			tvBroadcast.setBroadcasters(request.getParameter("broadcasters"));
			tvBroadcast.setSource_Of_Reference(request.getParameter("source_of_reference"));
			tvBroadcast = tvBroadcastService.checkEmptyValues(tvBroadcast);
			long broadcastId = Long.parseLong(request.getParameter("broadcastId"));
			int result = tvBroadcastService.updateTvBroadcast(tvBroadcast, broadcastId);
			if (result == 0)
			{
				TvBroadcasts tvBroadcasts = new TvBroadcasts();
				String country = request.getParameter("country");
				TvBroadcast tvQuery = new TvBroadcast();
				tvQuery.setCountry(country);
				tvBroadcasts = tvBroadcastService.viewTvBroadcastsNew(tvQuery);
				request.setAttribute("tvBroadcasts", tvBroadcasts);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/viewTvFullDetails.jsp");
				rd.forward(request, response);
			}
		}
	}
}